package com.jungle.bfi.controller;

import com.jungle.base.domain.vo.JsonResult;
import com.jungle.bfi.domain.bo.ApiPathBo;
import com.jungle.bfi.entity.ApiPathPo;
import com.jungle.bfi.service.metadata.ApiParsingService;
import com.jungle.bfi.service.metadata.ApiPathService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("bizSystem")
public class BizSystemController {

    @Resource
    private ApiPathService apiPathService;
    @Resource
    private ApiParsingService apiParsingService;

    @GetMapping(value = "findAll", produces = MediaType.APPLICATION_JSON_VALUE)
    public JsonResult<List<ApiPathPo>> findAll() {
        return JsonResult.success(apiPathService.findAll(null));
    }

    @PostMapping("delete")
    public JsonResult<Void> delete(@RequestBody ApiPathBo apiPathBo) {
        log.info("delete bizTreeBo:{}", apiPathBo);

        String id = apiPathBo.getId();
        if (id == null) {
            apiPathService.deleteAll(apiPathService.findAll(null));
        } else {
            apiPathService.deleteById(id);
        }

        return JsonResult.success();
    }

    @GetMapping("analyse")
    public JsonResult<Void> analyse(@RequestParam() String apiUrl) {
        log.info("analyse apiUrl:{}", apiUrl);

        // analyse
        apiParsingService.analyse(apiUrl);

        // return
        return JsonResult.success();
    }
}
